package com.xnyzc.lhy.mis.service.impl.cms.config;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.penum.SchedulingStrategyState;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.cms.config.OaSysSchedulingStrategy;
import com.xnyzc.lhy.mis.entity.param.cms.config.OaSysSchedulingStrategyParam;
import com.xnyzc.lhy.mis.mapper.config.OaSysSchedulingStrategyMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.mis.service.cms.config.IOaSysSchedulingStrategyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


/**
 * <p>
 * 调度策略 服务实现类
 * </p>
 *
 * @author baoyaze
 * @since 2019-10-23
 */
@Service
public class OaSysSchedulingStrategyServiceImpl
    extends ServiceImpl<OaSysSchedulingStrategyMapper, OaSysSchedulingStrategy>
    implements IOaSysSchedulingStrategyService
{

    @Override
    public Rv getSchedulingStrategyList(OaSysSchedulingStrategyParam schedulingStrategyParam)
    {
        IPage result = schedulingStrategyParam.getPage();
        Map<String, Object> params = new HashMap<String, Object>();
        if(StringUtils.isNotEmpty(schedulingStrategyParam.getCityId()))
        {
            params.put(OaSysSchedulingStrategyParam.CITY_ID, schedulingStrategyParam.getCityId());
        }
        result.setRecords(baseMapper.queryList(result, params));
        return Rv.wrap(EErrorCode.success, result);
    }

    @Override
    public Rv getSchedulingStrategyInfo(Long id)
    {
        return Rv.wrap(EErrorCode.success, baseMapper.selectById(id));
    }

    @Override
    @Transactional
    public Rv addSchedulingStrategy(OaSysSchedulingStrategy schedulingStrategy)
    {
        if(StringUtils.isNotEmpty(schedulingStrategy.getCityId()))
        {
            // ?可以设置通用方法，赋值 tag、createUser、createTime、updateTime?
            String tag = TokenUtil.getTag();
            if (CheckUtil.objIsEmpty(tag)) {
                throw PangException.create(EErrorCode.missingAuthArgs);
            }
            // 新增的调度策略，状态为停用
            schedulingStrategy.setStateFlag(SchedulingStrategyState.DISABLED.getValue());
            schedulingStrategy.setTag(tag);
            schedulingStrategy.setCreateUser(TokenUtil.getTokenUserId());
            schedulingStrategy.setCreateTime(new Date());
            schedulingStrategy.setUpdateTime(new Date());
            schedulingStrategy.setVersion(0L);
            schedulingStrategy.setDeleted(0);
            schedulingStrategy.setSchedulingStrategyId(IDUtil.nextId());
            baseMapper.insert(schedulingStrategy);
        }
        else
        {
            return Rv.error(EErrorCode.notCityIncashWithdrawalConfig);
        }

        return Rv.wrap(EErrorCode.success);
    }

    @Override
    @Transactional
    public Rv updateSchedulingStrategy(OaSysSchedulingStrategy schedulingStrategy)
    {
        baseMapper.updateById(schedulingStrategy);
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    @Transactional
    public Rv deleteSchedulingStrategy(OaSysSchedulingStrategy schedulingStrategy)
    {
        baseMapper.deleteById(schedulingStrategy.getSchedulingStrategyId());
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    @Transactional
    public Rv disabledSchedulingStrategy(OaSysSchedulingStrategy schedulingStrategy)
    {
        baseMapper.updateStateById(schedulingStrategy.getSchedulingStrategyId(), SchedulingStrategyState.DISABLED.getValue());
        return Rv.wrap(EErrorCode.success);
    }

    @Override
    @Transactional
    public Rv enabledSchedulingStrategy(OaSysSchedulingStrategy schedulingStrategy)
    {
        // 先根据条件，禁用其它调度策略
        Map<String, Object> params = new HashMap<>();
        params.put(OaSysSchedulingStrategyParam.CITY_ID, schedulingStrategy.getCityId());
        params.put(OaSysSchedulingStrategyParam.STATE_FLAG, SchedulingStrategyState.DISABLED.getValue());
        baseMapper.updateStateByParams(params);
        // 启用当前调度策略
        baseMapper.updateStateById(schedulingStrategy.getSchedulingStrategyId(), SchedulingStrategyState.ENABLED.getValue());
        return Rv.wrap(EErrorCode.success);
    }
}
